import config from "../app/config"
import  Jwt, { JsonWebTokenError, JwtPayload, TokenExpiredError }  from "jsonwebtoken"

function sign(data: any) {
    return Jwt.sign({admin: data}, config.jwt.secret as string, 
        { expiresIn: config.jwt.expire })
}

function verify(token: string): {admin: JwtPayload|string|null, error: TokenExpiredError|JsonWebTokenError|null} {
    try {
        const decoded = Jwt.verify(token, config.jwt.secret as string);
        return {
            admin: decoded,
            error: null
        }
    } catch (err) {
        return {
            admin: null,
            error: err as TokenExpiredError|JsonWebTokenError|null
        }
    }
}

export {
    sign,
    verify
}